package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.UIUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GridViewAdapter extends IBaseAdapter {
	private List<JSONObject> listItems;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public GridViewAdapter(Activity activity, List<JSONObject> listItems) {
		this.listItems = listItems;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
	}

	@Override
	public int getCount() {
		int count = listItems == null ? 0 : listItems.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return listItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public ImageView image;
		public TextView name,info;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_tandian_item = R.layout.gridview_item;
			convertView = LayoutInflater.from(appContext).inflate(main_tandian_item, null);
			holder.image = (ImageView) convertView.findViewById(R.id.downg_item_img);			
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		JSONObject item = listItems.get(position);
		try {	
			ImageLoader.getInstance().displayImage(
					"http://182.254.153.36/native"
							+ item.getString("image"),
							holder.image,
					UIUtil.getImageLoaderDisplayOptions(R.drawable.icon_default_f));
		    //holder.image.setImageResource(Integer.parseInt(item[2].toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}
}
